package com.yf.exam.modules.trainingarchives.evaluation.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yf.exam.core.api.dto.PagingReqDTO;
import com.yf.exam.modules.trainingarchives.evaluation.dto.EvaluationInfoDTO;
import com.yf.exam.modules.trainingarchives.evaluation.dto.EvaluationScoreDTO;
import com.yf.exam.modules.trainingarchives.evaluation.entity.EvaluationInfo;
import com.yf.exam.modules.trainingarchives.evaluation.entity.EvaluationScore;
import com.yf.exam.modules.trainingarchives.evaluation.mapper.EvaluationInfoMapper;
import com.yf.exam.modules.trainingarchives.evaluation.service.IEvaluationInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yf.exam.modules.trainingarchives.evaluation.service.IEvaluationScoreService;
import com.yf.exam.modules.trainingarchives.evaluation.service.IEvaluationService;
import com.yf.exam.modules.trainingarchives.evaluation.vo.EvaluationInfoListVO;
import com.yf.exam.modules.trainingarchives.evaluation.vo.EvaluationItemVO;
import com.yf.exam.modules.trainingarchives.evaluation.vo.EvaluationVO;
import com.yf.exam.modules.trainingarchives.pdfexport.dto.second.EvaluationInfoExportDTO;
import com.yf.exam.modules.trainingarchives.teacherattendance.vo.TeacherAttendanceListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 满意度调查详情表 服务实现类
 * </p>
 *
 * @author lql
 * @since 2025-01-06
 */
@Service
public class EvaluationInfoServiceImpl extends ServiceImpl<EvaluationInfoMapper, EvaluationInfo> implements IEvaluationInfoService {

    @Resource
    private EvaluationServiceImpl evaluationService;
    @Resource
    private IEvaluationScoreService iEvaluationScoreService;
    @Override
    public void add(EvaluationInfoDTO reqDTO) {

        EvaluationInfo evaluationInfo = new EvaluationInfo();
        List<EvaluationScoreDTO> evaluationScoreDTOList = reqDTO.getEvaluationScoreDTOList();
        BeanUtils.copyProperties(reqDTO, evaluationInfo);
        evaluationInfo.setId(IdWorker.getIdStr());
        evaluationInfo.setEvaluationScore(evaluationScoreDTOList.stream().mapToInt(EvaluationScoreDTO::getEvaluationItemScore).sum());
        List<EvaluationScore> evaluationScores = evaluationScoreDTOList.stream().map(evaluationScoreDTO -> {
            EvaluationScore evaluationScore = new EvaluationScore();
            BeanUtils.copyProperties(evaluationScoreDTO, evaluationScore);
            evaluationScore.setEvaluationInfoId(evaluationInfo.getId());
            return evaluationScore;
        }).collect(Collectors.toList());

        this.save(evaluationInfo);
        iEvaluationScoreService.saveBatch(evaluationScores);
    }

    @Override
    public IPage<EvaluationInfoListVO> paging(PagingReqDTO<EvaluationInfoListVO> reqDTO) {

        return baseMapper.paging(reqDTO.toPage(), reqDTO.getParams());
    }

    @Override
    public List<EvaluationInfoExportDTO> getEvaluationInfoExportDTOList(Map<String, Object> queryMap) {
        return baseMapper.getEvaluationInfoExportDTOList(queryMap);
    }

    @Override
    public EvaluationVO getDetails(String id) {
        EvaluationVO evaluationVO = baseMapper.getDetails(id);
        List<EvaluationItemVO>  evaluationItemVO= evaluationVO.getEvaluationItemVOs();
        evaluationVO.setEvaluationItemVO(evaluationService.buildTree(evaluationItemVO));
        evaluationVO.setEvaluationItemVOs(null);
        return evaluationVO;
    }

}
